<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <title>JThread Test</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <script type="text/javascript" src="JThread.js"></script>
        <script type="text/javascript">
            window.onload = function(){
                var ndLogReg = document.getElementById("logDivReg");
                var ndLogThr = document.getElementById("logDivThr");
                var ndLogArr = document.getElementById("logDivArr");
                var ndIterTxt = document.getElementById("iterTxt");

                document.getElementById("runAllBtn").onclick = function(){
                    document.getElementById("runArrayBtn").click();
                    document.getElementById("runThreadBtn").click();
                    document.getElementById("runRegularBtn").click();
                };

                var threadKillArr = [];
                document.getElementById("killBtn").onclick = function(){
                    while(threadKillArr.length){
                        threadKillArr.pop().kill();//kill thread
                    }
                }
                
                document.getElementById("cleanBtn").onclick = function(){
                    ndLogReg.innerHTML = "";
                    ndLogThr.innerHTML = "";
                    ndLogArr.innerHTML = "";
                }

                var appendMsg = function(ndLog,msg,color){
                    var newChild = document.createElement("div");
                    if(color){
                        newChild.style.color = color;
                    }
                    newChild.innerHTML = msg;
                    ndLog.appendChild(newChild);
                    ndLog.scrollTop = 999999;
                };


                document.getElementById("runRegularBtn").onclick = function(){
                    var iMax = ndIterTxt.value;
                    var start = new Date();
                    //regular loop
                    for(var i=0;i<iMax;i++){
                        appendMsg(ndLogReg,"Regular | "+i);
                    }
                    var end = new Date();
                    appendMsg(ndLogReg, "ID: N/A | Time: "+((end.getTime() - start.getTime())/1000)+" sec","red");
                };
                
                document.getElementById("runThreadBtn").onclick = function(){
                    //prepare JThread loop parts
                    var loopBody, exitCondition, onEnd, onKill, i = 0, iMax = ndIterTxt.value;
                    loopBody = function(t){
                        appendMsg(ndLogThr,"JThread | " + t.getThreadId() + " | " + i++);
                    };
                    exitCondition = function(t){
                        return i<iMax;//exit condition
                    };
                    onEnd = onKill = function(t){
                        appendMsg(ndLogThr,"ID: " + t.getThreadId() + " | Time: " + t.getTime() + " sec","red");
                    };
                    //create and run JThread loop object
                    var jThread = new JThread(loopBody, exitCondition, onEnd, onKill).run();
                    
                    threadKillArr.push(jThread);
                };
                
                document.getElementById("runArrayBtn").onclick = function(){
                    //create a dummy array and populate with dummy values
                    var arr = [], iMax = ndIterTxt.value, i = iMax;
                    while(i){
                        arr.push("item#"+(iMax - i--));
                    }

                    //prepare JThread loop parts
                    var callback, onEnd, onKill;
                    callback = function(t, item, idx){
                        appendMsg(ndLogArr, "JThread forEach | " + t.getThreadId() + " | " + item + " | " + idx);
                    }
                    onEnd = onKill = function(t){
                        appendMsg(ndLogArr,"ID: " + t.getThreadId() + " | Time: " + t.getTime() + " sec","red");
                    };
                    //run JThread forEach loop
                    var jThread = JThread.forEach(arr, callback, onEnd, onKill);

                    threadKillArr.push(jThread);
                };
            }
        </script>
    </head>
    <body>

        <table border="0" cellspacing="2" cellpadding="0">
            <tbody>
                <tr>
                    <td>
                        <b>Loop Iterations:</b>
                    </td>
                    <td>
                        <input id="iterTxt" type="text" value="1000" />
                    </td>
                </tr>
                <tr>
                    <td>
                        <b>Freeze Indicator:</b>
                    </td>
                    <td>
                        <img alt="performance test" src="spin_loader.gif" />
                    </td>
                </tr>
            </tbody>
        </table>

        <table border="0" cellspacing="2" cellpadding="0">
            <tbody>
                <tr>
                    <td>
                        <input id="runAllBtn" type="button" value="Run All" />
                        <input id="killBtn" type="button" value="Kill JThreads" />
                        <input id="cleanBtn" type="button" value="Clean" />
                    </td>
                </tr>
            </tbody>
        </table>
        <br />
        <table border="0" cellspacing="2" cellpadding="0">
            <tbody>
                <tr>
                    <td align="center">
                        <input id="runRegularBtn" type="button" value="Run Regular Loop" />
                    </td>
                    <td align="center">
                        <input id="runThreadBtn" type="button" value="Run JThread Loop" />
                    </td>
                    <td align="center">
                        <input id="runArrayBtn" type="button" value="Run JThread forEach Loop" />
                    </td>
                </tr>
                <tr>
                    <td>
                        <div id="logDivReg" style="border:1px solid brown;height:400px;width:350px;;overflow:auto;"></div>
                    </td>
                    <td>
                        <div id="logDivThr" style="border:1px solid green;height:400px;width:350px;;overflow:auto;"></div>
                    </td>
                    <td>
                        <div id="logDivArr" style="border:1px solid green;height:400px;width:350px;;overflow:auto;"></div>
                    </td>
                </tr>
            </tbody>
        </table>

    </body>
</html>
